<?php
/**
* @file $Id: GetMP.fnc.php 161 2006-09-07 06:21:17Z doritojones $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

function GetMP($mp,$column='TITLE')
{	global $_FOCUS;

	if(!$_FOCUS['GetMP'])
	{
		$_FOCUS['GetMP'] = array();
		$arr = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,TITLE,YEAR_FRACTION,POST_START_DATE,POST_END_DATE,REGISTRATION_START_DATE,REGISTRATION_END_DATE,'SCHOOL_QUARTERS' AS TABLE,SORT_ORDER,SHORT_NAME,START_DATE,END_DATE FROM SCHOOL_QUARTERS"),array(),array('MARKING_PERIOD_ID'));
		if(is_array($arr))
			$_FOCUS['GetMP'] += $arr;

		$arr = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,TITLE,YEAR_FRACTION,POST_START_DATE,POST_END_DATE,REGISTRATION_START_DATE,REGISTRATION_END_DATE,'SCHOOL_SEMESTERS' AS TABLE,SORT_ORDER,SHORT_NAME,START_DATE,END_DATE FROM SCHOOL_SEMESTERS"),array(),array('MARKING_PERIOD_ID'));
		if(is_array($arr))
			$_FOCUS['GetMP'] += $arr;

		$arr = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,TITLE,YEAR_FRACTION,POST_START_DATE,POST_END_DATE,REGISTRATION_START_DATE,REGISTRATION_END_DATE,'SCHOOL_YEARS' AS TABLE,SORT_ORDER,SHORT_NAME,START_DATE,END_DATE FROM SCHOOL_YEARS"),array(),array('MARKING_PERIOD_ID'));
		if(is_array($arr))
			$_FOCUS['GetMP'] += $arr;

		$arr = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,TITLE,POST_START_DATE,POST_END_DATE,'SCHOOL_PROGRESS_PERIODS' AS TABLE,SORT_ORDER,SHORT_NAME,START_DATE,END_DATE FROM SCHOOL_PROGRESS_PERIODS"),array(),array('MARKING_PERIOD_ID'));
		if(is_array($arr))
			$_FOCUS['GetMP'] += $arr;
	}
	if(substr($mp,0,1)=='E' && $column=='TITLE')
	{
		$suffix = ' Exam';
		$mp = substr($mp,1);
	}

	if($mp==0 && $column=='TITLE')
		return 'Full Year';
	else
		return $_FOCUS['GetMP'][$mp][1][$column].$suffix;
}

function GetMPTable($mp_table)
{
	switch($mp_table)
	{
		case 'SCHOOL_SEMESTERS':
			return 'SEM';
		break;
		case 'SCHOOL_QUARTERS':
			return 'QTR';
		break;
		case 'SCHOOL_PROGRESS_PERIODS':
			return 'PRO';
		break;
		default:
			return 'FY';
		break;
	}
}

function GetCurrentMP($mp,$date,$error=true)
{	global $_FOCUS;

	switch($mp)
	{
		case 'FY':
			$table = 'SCHOOL_YEARS';
		break;

		case 'SEM':
			$table = 'SCHOOL_SEMESTERS';
		break;

		case 'QTR':
			$table = 'SCHOOL_QUARTERS';
		break;

		case 'PRO':
			$table = 'SCHOOL_PROGRESS_PERIODS';
		break;
	}

	if(!$_FOCUS['GetCurrentMP'][$date][$mp])
	 	$_FOCUS['GetCurrentMP'][$date][$mp] = DBGet(DBQuery("SELECT MARKING_PERIOD_ID FROM $table WHERE '$date' BETWEEN START_DATE AND END_DATE AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));

	if($_FOCUS['GetCurrentMP'][$date][$mp][1]['MARKING_PERIOD_ID'])
		return $_FOCUS['GetCurrentMP'][$date][$mp][1]['MARKING_PERIOD_ID'];
	elseif(strpos($_SERVER['PHP_SELF'],'Side.php')===false && $error==true)
		echo ErrorMessage(array(_("You are not currently in a marking period")),'fatal');
}
?>